package com.hcbd.tdwt.ui.xingyu.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.GlobalParams;

import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;

import java.text.MessageFormat;
import java.util.List;

public class ContactMsgAdapter extends RecyclerView.Adapter<ContactMsgAdapter.MyViewHolder> {
    protected final Context mContext;
    private List<BoxContact> boxContacts;
    private final LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    PopupMenu popupMenu;
    private Drawable touchOnDrawable;
    private Drawable touchPreDrawable;
    private Drawable avatarDb;
    private Drawable centerAvatarDb;
    private int resource;
    public ContactMsgAdapter(Context context, List<BoxContact> list) {
        mContext = context;
        mInflater = ((Activity) context).getLayoutInflater();
        boxContacts = list;
        avatarDb= ContextCompat.getDrawable(mContext, R.drawable.avatar);
        centerAvatarDb= ContextCompat.getDrawable(mContext,R.drawable.command_center_avatar);
        if(BdToolFactory.app_type==BdToolFactory.FU_DA_BG_TYPE){
            touchPreDrawable=mContext.getDrawable(R.color.white);
            touchOnDrawable=mContext.getDrawable(R.drawable.layout_frame_while);
            resource=R.layout.contact_msg_item_fuda;
        }else if(BdToolFactory.app_type==BdToolFactory.HAI_GE_BG_TYPE){
            touchPreDrawable=mContext.getDrawable(R.drawable.layout_frame_while_ff);
            touchOnDrawable=mContext.getDrawable(R.drawable.layout_frame_grey);
            avatarDb= ContextCompat.getDrawable(mContext,R.drawable.default_avatar_haige);
            centerAvatarDb= ContextCompat.getDrawable(mContext,R.drawable.center_avatar_haige);
            resource=R.layout.contact_msg_item_haige;
        }else if(BdToolFactory.app_type==BdToolFactory.HLCT_BG_TYPE){
            touchPreDrawable=mContext.getDrawable(R.drawable.layout_frame_while_ff);
            touchOnDrawable=mContext.getDrawable(R.drawable.layout_frame_grey);
            avatarDb= ContextCompat.getDrawable(mContext,R.drawable.defult_avatar_tt);
            centerAvatarDb= ContextCompat.getDrawable(mContext,R.drawable.center_avatar_tt);
            resource=R.layout.contact_msg_item_fuda;
        }else{
            touchPreDrawable=mContext.getDrawable(R.drawable.layout_frame_light_black);
            touchOnDrawable=mContext.getDrawable(R.drawable.layout_frame_while);
            resource=R.layout.contact_msg_item;
        }

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view =mInflater.inflate(resource, viewGroup, false);
        return new MyViewHolder(view);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        BoxContact boxContact = boxContacts.get(i);
//        Log.e("TAG", "时间:"+boxContact.getRecentCommunicationTime());
        myViewHolder.timeTv.setText(DateUtils.simplifyTime(boxContact.getRecentCommunicationTime()));
        myViewHolder.msgTv.setText(boxContact.getLastTimeContent());
        int unRead=boxContact.getUnReadNewsNum();
        if(unRead>0){
            myViewHolder.unreadMsg.setVisibility(View.VISIBLE);
        }else{
            myViewHolder.unreadMsg.setVisibility(View.GONE);
        }
        myViewHolder.unreadMsg.setText(String.valueOf(unRead));
        if(GlobalParams.commandCentreNumber.equals(boxContact.getBoxId())){
            myViewHolder.avatarImg.setBackground(centerAvatarDb);
            myViewHolder.contactLL.setOnTouchListener(null);
            myViewHolder.contactLL.setOnLongClickListener(null);
            myViewHolder.contactName.setText(boxContact.getName());
        }else{
            myViewHolder.contactName.setText(MessageFormat.format("{0}({1})", boxContact.getName(), boxContact.getBoxId()));
            myViewHolder.avatarImg.setBackground(avatarDb);
            myViewHolder.contactLL.setOnLongClickListener(v -> {
                popupMenu = new PopupMenu(mContext,v);
                popupMenu.getMenuInflater().inflate(R.menu.chat_menu,popupMenu.getMenu());
                //弹出式菜单的菜单项点击事件
                popupMenu.setOnMenuItemClickListener(item -> {
                    initPopupMenu(item,boxContact);
                    return false;
                });
                popupMenu.show();
                popupMenu.setOnDismissListener(popupMenu -> {
                    v.setBackground(touchPreDrawable);
                });
                return false;
            });
            myViewHolder.contactLL.setOnTouchListener((v, event) -> {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    v.setBackground(touchOnDrawable);
                }else if(event.getAction() == MotionEvent.ACTION_CANCEL){
                    v.setBackground(touchPreDrawable);
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    v.setBackground(touchPreDrawable);
                }
                return false;
            });
        }
        myViewHolder.contactLL.setOnClickListener(view -> {
            onItemClickListener.onClick(boxContact,view);
        });
    }

    public void initPopupMenu(MenuItem item, BoxContact boxContact){
        if(item.getTitle().toString().equals("删除")){
            onItemClickListener.onDel(boxContact);
        }else if(item.getTitle().toString().equals("编辑")){
            onItemClickListener.onEdit(boxContact);
        }
    }
    @Override
    public int getItemCount() {
        return boxContacts.size();
    }

    public void notifyDataSetChanged(List<BoxContact> list) {
        boxContacts = list;
        notifyDataSetChanged();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView contactName,msgTv,timeTv;
        LinearLayout contactLL;
        ImageView avatarImg;
        TextView unreadMsg;
        View line;
        MyViewHolder(View itemView) {
            super(itemView);
            contactName=itemView.findViewById(R.id.contactName);
            msgTv=itemView.findViewById(R.id.msg_tv);
            contactLL=itemView.findViewById(R.id.contactLL);
            timeTv=itemView.findViewById(R.id.time_tv);
            avatarImg=itemView.findViewById(R.id.avatarImg);
            unreadMsg=itemView.findViewById(R.id.unread_msg);
            line=itemView.findViewById(R.id.line);
        }
    }

    public interface OnItemClickListener {
        void onClick(BoxContact boxContact,View view);
        void onEdit(BoxContact boxContact);
        void onDel(BoxContact boxContact);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
